package com.jiazhong.front.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.commons.Mapper.CategoryMapper;
import com.jiazhong.commons.Util.JsonResult;
import com.jiazhong.commons.Util.ResultTool;
import com.jiazhong.commons.bean.Category;
import com.jiazhong.front.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public JsonResult findAll() {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_CATEGORY");
        if (json == null) {
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.last("limit 8");
            List<Category> list = list(wrapper);
            ops.set("FRONT_INDEX_CATEGORY", JSONArray.toJSONString(list), 1, TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Category> list = JSONArray.parseArray(json, Category.class);
        return ResultTool.success(list);
    }
}
